package cn.ys.groupbuy;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import cn.ys.others.NoScrollListview;
import cn.ys.refresh.PullToRefreshScrollView;

/**
 * Created by shao on 2015/6/11.
 */
public class CateActivity extends Activity implements View.OnClickListener,PopupWindow.OnDismissListener{

    private NoScrollListview listview;
    private ImageButton cate_close,cate_back;
    private ViewPager viewPager;
    private RelativeLayout cate_banner;
    private ScrollView cateScrollView;
    private PullToRefreshScrollView refreshScrollView;
    private RelativeLayout toggle_first,toggle_sec,toggle_third,toggle_fourth;
    private LinearLayout toggle_layout;
    private CateView cateView;
    private int displayWidth,displayHeight;
    private boolean isChecked=false;

    private PopupWindow popupWindow;
    private View popupView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tuan_cate);

        displayWidth=getWindowManager().getDefaultDisplay().getWidth();
        displayHeight=getWindowManager().getDefaultDisplay().getHeight();

        cateView=new CateView(this);

        cateView.setBackgroundColor(0xb0000000);


        listview=(NoScrollListview)findViewById(R.id.cate_list);
        listview.setAdapter(new MyAdapter(this));
        cate_close=(ImageButton)findViewById(R.id.cate_close);
        cate_close.setOnClickListener(this);
        cate_back=(ImageButton)findViewById(R.id.ic_back);
        cate_back.setOnClickListener(this);
        viewPager=(ViewPager)findViewById(R.id.cate_viewpager);
        cate_banner=(RelativeLayout)findViewById(R.id.cate_banner);
        refreshScrollView=(PullToRefreshScrollView)findViewById(R.id.cate_scrollview);
        cateScrollView=refreshScrollView.getRefreshableView();
        //cate_banner.requestFocus();
        cate_banner.setFocusable(true);
        cate_banner.setFocusableInTouchMode(true);

        toggle_layout=(LinearLayout)findViewById(R.id.toggle_layout);
        toggle_first=(RelativeLayout)findViewById(R.id.toggle_first);
        toggle_first.setOnClickListener(this);
        toggle_sec=(RelativeLayout)findViewById(R.id.toggle_sec);
        toggle_sec.setOnClickListener(this);
        toggle_third=(RelativeLayout)findViewById(R.id.toggle_third);
        toggle_third.setOnClickListener(this);
        toggle_fourth=(RelativeLayout)findViewById(R.id.toggle_fourth);
        toggle_fourth.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == cate_close) {
            cate_banner.setVisibility(View.GONE);
        }
        if(v==cate_back){
            finish();
        }
        if(v==toggle_first){
            popWindow();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void popWindow(){
        System.out.println("shao--------->>>> popWindow method isChecked value is : "+isChecked);
     //   if(popupWindow==null) {
            System.out.println("shao--------->>>>> popupWindow value is null");
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            //popupView = inflater.inflate(R.layout.popupwindow_layout, null, false);
            popupWindow = new PopupWindow(cateView, displayWidth, displayHeight);
            popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);
            //popupWindow.update();

           // popupWindow.setBackgroundDrawable(new BitmapDrawable());

      //  }
        popupWindow.getContentView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.setFocusable(false);
                popupWindow.dismiss();
                isChecked=false;
                return true;
            }
        });
        if(!isChecked){
            isChecked=true;
           // cateView.setFocusable(true);
           // cateView.setFocusableInTouchMode(true);
            popupWindow.showAsDropDown(toggle_layout,0,0);
           // popupWindow.update(0,200,displayWidth,displayHeight);
            return;
        }else {
           // System.out.println("shao----------->>>>popWindow method isChecked value is true");
            popupWindow.dismiss();
            //popupWindow.setOnDismissListener(this);
           // popupWindow=null;
            isChecked=false;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(popupWindow != null){
            popupWindow.dismiss();
            popupWindow=null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(popupWindow != null){
            popupWindow.dismiss();
            popupWindow=null;
        }
    }

    @Override
    public void onDismiss() {
       // popupWindow.showAsDropDown(toggle_layout,0,0);
       // popupWindow.setOnDismissListener(null);
    }

    class MyAdapter extends BaseAdapter {

        Context mContext;
        LayoutInflater inflater;

        public MyAdapter(Context context){
            mContext=context;
            inflater=LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView=inflater.inflate(R.layout.cate_list_item,null);

            return convertView;
        }
    }
}
